package com.pml.util;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.util.*;

public class FileUploadUtil {
    private HttpServletRequest request;
    private ServletFileUpload upload;
    private Map<String,List<FileItem>> map;
    private Map<String,List<String>> params;
    private Map<String,FileItem> files;


   public FileUploadUtil(HttpServletRequest request,long size,String tempDir){
       DiskFileItemFactory factory=new DiskFileItemFactory();
       factory.setRepository(new File(tempDir));
       upload=new ServletFileUpload(factory);
       upload.setHeaderEncoding("utf-8");
       upload.setFileSizeMax(size);
       try {
           map=upload.parseParameterMap(request);
       } catch (FileUploadException e) {
           e.printStackTrace();
       }
       init();


   }

   public void init(){
       Set<String> set= map.keySet();
       Iterator<String> iterator=set.iterator();
       while (iterator.hasNext()){
           String name=iterator.next();
           List<FileItem> fileItemList=map.get(name);
           if(fileItemList.get(0).isFormField()){
               List<String> list=new ArrayList<>();
               for (int i=0;i<fileItemList.size();i++){
                   list.add(fileItemList.get(i).getString());
               }
               if (params==null){
                   params=new HashMap<>();
               }
                params.put(name,list);
           }
           else{
               if (files==null){
                   files=new HashMap<>();
               }
               files.put(name,fileItemList.get(0));
           }
       }

   }

   public String getParameter(String name){
       if(params.containsKey(name)){
           return params.get(name).get(0);
       }
       else {
           return null;
       }
   }
   public List<String> getParameterValues(String name){
       if (params.containsKey(name)){
           return params.get(name);
       }
       else {
           return null;
       }
   }


   public List<String> saveAll(String saveDir){
       List<String> list=new ArrayList<>();
       Set<String> set=files.keySet();
       Iterator<String> iterator=set.iterator();
       InputStream inputStream=null;
       OutputStream outputStream=null;
       while (iterator.hasNext()){
           FileItem fileItem=files.get(iterator.next());
           IpTimeStamp ipTimeStamp=new IpTimeStamp("199.32.42.3");
           String fileName=ipTimeStamp.getRandomName()+"."+fileItem.getName().split("\\.")[fileItem.getName().split("\\.").length-1];
           try {
               inputStream=fileItem.getInputStream();
               outputStream= new FileOutputStream(new File(saveDir+File.separator+fileName));
               byte[] bytes=new byte[512];
               int temp=0;
               while ((temp=inputStream.read(bytes))!=-1){
                   outputStream.write(bytes);
               }
               list.add(fileName);

           } catch (IOException e) {
               e.printStackTrace();
           }finally {
               try {
                   inputStream.close();
                   outputStream.close();
               } catch (IOException e) {
                   e.printStackTrace();
               }

           }

       }
       return list;
   }

    public static void main(String[] args) {

    }


}
